<template>
  <el-form
    ref="userForm"
    :model="userInfo"
    :rules="rulesUserForm"
    label-width="120px"
    style="margin-left: 120px; margin-top: 30px"
  >
    <el-form-item label="姓名:" prop="username">
      <el-input v-model="userInfo.username" style="width: 300px" />
    </el-form-item>
    <el-form-item label="密码:" prop="password">
      <el-input
        v-model="userInfo.password"
        type="password"
        style="width: 300px"
      />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="setPassword">更新</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { getUserDetailById, saveUserDetailById } from '@/api/user'
export default {
  data() {
    return {
      userInfo: {
        username: '',
        password: ''
      },
      rulesUserForm: {
        username: [
          { required: true, message: '用户名必填！！', trigger: 'blur' }
        ],
        password: [{ required: true, message: '密码必填！！', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getUserDetail()
  },
  methods: {
    async getUserDetail() {
      const res = await getUserDetailById(this.$route.query.id)
      this.userInfo = res.data
      //   console.log(this.list)
    },
    // 修改登录账号和密码
    setPassword() {
      this.$refs.userForm.validate(async(valid) => {
        if (!valid) return
        const res = await saveUserDetailById(this.userInfo)
        this.$message.success(res.message)
      })
    }
  }
}
</script>
